var prefix = jsctx + "/tenantUgsLevelType";

var levelDiscountStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageLevelDiscountStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            stationName: $("#stationName").val().trim(),
                            bizType: $("#bizType").val().trim(),
                            status: $("#status").val().trim(),
                            validStatus: $("#validStatus").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '桩群', align: 'left'},
                        {
                            field: 'bizType', title: '桩群性质',
                            align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '自营'
                                } else if (value == '1') {
                                    return '非自营 '
                                }
                            }
                        },
                        {
                            field: 'status', title: '启用状态',
                            align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '启动'
                                } else if (value == '1') {
                                    return '停止 '
                                }
                            }
                        },
                        {field: 'validStatus', title: '有效期状态', align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            formatter: function (value, row, index) {

                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="折扣设置" onclick="levelDiscountStation.editStationDiscount(\''
                                    + row.elecStationId
                                    + '\')">折扣设置</a> ';

                                var b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="修改记录" onclick="levelDiscountStation.modifyRecord(\''
                                    + row.elecStationId
                                    + '\')">修改记录</a> ';

                                return a + b;

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    function goBack() {
        window.history.back();
    }

    function editStationDiscount(elecStationId) {
        location.href = prefix + "/editStationDiscount/" + elecStationId;

    }

    function modifyRecord(elecStationId) {
        location.href = prefix + "/modifyRecord/" + elecStationId;
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        editStationDiscount: editStationDiscount,
        modifyRecord: modifyRecord

    }
        ;
})();

var editLevelDiscountStation = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() { if (isajaxsend) {
            var formData = new FormData($("#frmLevelDiscountStation")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateLevelDiscountStation",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmLevelDiscountStation").validate({
            rules: {
                growingTypeName: {
                    required: true
                },
                experienceValue: {
                    required: true
                },
                experienceTop: {
                    required: true
                }
            },
            messages: {
                growingTypeName: {
                    required: icon + "请输入名称"
                },
                experienceValue: {
                    required: icon + "请输入经验值"
                },
                experienceTop: {
                    required: icon + "请输入每日经验封顶值"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
